package org.tacademy.mypagesliding;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;

public class MyPageSlidingActivity extends Activity {

	boolean isPageOpen = true;
	Animation leftAnim;
	Animation rightAnim;
	
	LinearLayout slidingPage01;
	Button openBtn;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        slidingPage01 = (LinearLayout)findViewById(R.id.slidingPage01);
        openBtn = (Button)findViewById(R.id.openBtn01);
        
        leftAnim = AnimationUtils.loadAnimation(this, R.anim.translate_left);
        rightAnim = AnimationUtils.loadAnimation(this, R.anim.translate_right);
        
        SlidingAnimationListener animationListener = new SlidingAnimationListener();
        leftAnim.setAnimationListener(animationListener);
        rightAnim.setAnimationListener(animationListener);
        
        openBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(isPageOpen){
					slidingPage01.startAnimation(rightAnim);
				}
				else{
					slidingPage01.startAnimation(leftAnim);
					slidingPage01.setVisibility(View.VISIBLE);
				}
			}
		});
        
    }
	
	class SlidingAnimationListener implements AnimationListener{

		public void onAnimationEnd(Animation animation) {
			if(isPageOpen){
				slidingPage01.setVisibility(View.INVISIBLE);
				openBtn.setText("Open");
				isPageOpen=false;
			}
			else{
				openBtn.setText("Close");
				isPageOpen=true;
			}
		}

		public void onAnimationRepeat(Animation animation) {
			
		}

		public void onAnimationStart(Animation animation) {
			
		}
		
	}
}